from django.shortcuts import render
from .Ser import *
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.

class Teacher_View(APIView):
    def get(self,request):
        data=Teacher_M.objects.all()
        data_ser=Teacher_SER(data,many=True)
        return Response(data_ser.data)


class Student_View(APIView):
    def get(self,request):
        id=request.GET.get("id")
        if id:
            data = Student_M.objects.get(pk=id)
            data_ser=Student_SER(data)
            return Response(data_ser.data)
        else:
            data=Student_M.objects.all()
            data_ser = Student_SER(data,many=True)
            return Response(data_ser.data)


    def post(self,request):
        def post(self, request):
            data = Student_SER(data=request.data)
            if data.is_valid():
                data.save()
                return Response({"meg": "添加成功"})
            else:
                return Response({"meg": "添加失败"})


    def delete(self,request):
        id = request.GET.get("id")
        Student_M.objects.get(pk=id).delete()
        return Response({"meg":"删除成功"})


